package pers.hxy.lookweather.presenter.impl;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.geo.GeoBean;
import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.qweather.sdk.bean.weather.WeatherHourlyBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.QWeather;

import pers.hxy.lookweather.bean.AqiNowBean;
import pers.hxy.lookweather.model.WeatherModel;
import pers.hxy.lookweather.model.impl.WeatherModelImpl;
import pers.hxy.lookweather.presenter.WeatherListener;
import pers.hxy.lookweather.presenter.WeatherPresenter;
import pers.hxy.lookweather.utils.StringUtil;
import pers.hxy.lookweather.view.WeatherView;

public class WeatherPresenterImpl extends Activity implements WeatherPresenter, WeatherListener {
    private static final String TAG = "WeatherPresenterImpl";
    private WeatherView view;
    private WeatherModel model;

    public WeatherPresenterImpl(WeatherView view) {
        this.view = view;
        model = new WeatherModelImpl();
        Log.d(TAG, "constructor");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
    }

    @Override
    public void destroy() {
        Log.d(TAG, "destroy");
        view = null;
        model = null;
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy");
        super.onDestroy();
        destroy();
    }

    @Override
    public void getGeo(String location_id) {
        Log.d(TAG, "getGeo: " + location_id);
        QWeather.getGeoCityLookup(view.getContext(), location_id, new QWeather.OnResultGeoListener() {
            @Override
            public void onError(Throwable throwable) {
                Log.d(TAG, "getGeo onError " + throwable.toString());
                runOnUiThread(() -> {
                    view.showError("getGeo onError");
                });
            }

            @Override
            public void onSuccess(GeoBean geoBean) {
                Log.d(TAG, "getGeo onSuccess, code=" + geoBean.getCode());
                runOnUiThread(() -> {
                    if (Code.OK == geoBean.getCode()) {
                        view.getGeo(geoBean);
                        view.showSuccess("getGeo onSuccess");
                    } else {
                        view.showError("getGeo onError");
                    }
                });
            }
        });
    }

    @Override
    public void getWeatherNow(String location_id) {
        Log.d(TAG, "getWeatherNow: " + location_id);
        QWeather.getWeatherNow(view.getContext(), location_id, new QWeather.OnResultWeatherNowListener() {
            @Override
            public void onError(Throwable throwable) {
                Log.d(TAG, "getWeatherNow onError: " + throwable.toString());
                runOnUiThread(() -> {
                    view.showError("getWeatherNow onError");
                });
            }

            @Override
            public void onSuccess(WeatherNowBean weatherNowBean) {
                Log.d(TAG, "getWeatherNow onSuccess, code=" + weatherNowBean.getCode());
                runOnUiThread(() -> {
                    if (Code.OK == weatherNowBean.getCode()) {
                        view.getWeatherNow(weatherNowBean);
                        view.showSuccess("getWeatherNow onSuccess");
                    } else {
                        view.showError("getWeatherNow onError");
                    }
                });
            }
        });
    }

    @Override
    public void getWeather24H(String location_id) {
        Log.d(TAG, "getWeather24H: " + location_id);
        QWeather.getWeather24Hourly(view.getContext(), location_id, new QWeather.OnResultWeatherHourlyListener() {
            @Override
            public void onError(Throwable throwable) {
                Log.d(TAG, "getWeather24H onError: " + throwable.toString());
                runOnUiThread(() -> {
                    view.showError("getWeather24H onError");
                });
            }

            @Override
            public void onSuccess(WeatherHourlyBean weatherHourlyBean) {
                Log.d(TAG, "getWeather24H onSuccess, code=" + weatherHourlyBean.getCode());
                runOnUiThread(() -> {
                    if (Code.OK == weatherHourlyBean.getCode()) {
                        view.getWeather24H(weatherHourlyBean);
                        view.showSuccess("getWeather24H onSuccess");
                    } else {
                        view.showError("getWeather24H onError");
                    }
                });
            }
        });
    }

    @Override
    public void getWeather7D(String location_id) {
        Log.d(TAG, "getWeather7D: " + location_id);
        QWeather.getWeather7D(view.getContext(), location_id, new QWeather.OnResultWeatherDailyListener() {
            @Override
            public void onError(Throwable throwable) {
                Log.d(TAG, "getWeather7D onError: " + throwable.toString());
                runOnUiThread(() -> {
                    view.showError("getWeather7D");
                });
            }

            @Override
            public void onSuccess(WeatherDailyBean weatherDailyBean) {
                Log.d(TAG, "getWeather7D onSuccess, code=" + weatherDailyBean.getCode());
                runOnUiThread(() -> {
                    if (Code.OK == weatherDailyBean.getCode()) {
                        view.getWeather7D(weatherDailyBean);
                        view.showSuccess("getWeather7D onSuccess");
                    } else {
                        view.showError("getWeather7D");
                    }
                });
            }
        });
    }

    @Override
    public void getAqiNow(double latitude, double longitude) {
        Log.d(TAG, "getAqiNow: latitude=" + latitude + ", longitude=" + longitude);
        model.getAqiNow(latitude, longitude, this);
    }

    @Override
    public void onGetAqiNow(AqiNowBean aqiNowBean) {
        Log.d(TAG, "onGetAqiNow: " + StringUtil.isNull(aqiNowBean));
        runOnUiThread(() -> {
            if (aqiNowBean == null) {
                view.showError("onGetAqiNow error");
            } else {
                view.getAqiNow(aqiNowBean);
                view.showSuccess("onGetAqiNow success");
            }
        });
    }
}
